package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.db.domain.MarketIssueExample;
import com.cskaoyan.market.db.mapper.MarketIssueMapper;
import com.cskaoyan.market.service.AdminIssueService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class AdminIssueServiceImpl implements AdminIssueService {

    //list方法
    @Override
    public List<MarketIssue> list(String page, String limit, String sort, String order, String question) {
        //获得session
        SqlSession session = MybatisUtil.getSession();
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria or = marketIssueExample.or();

        //判断是否有查找
        if (!StringUtils.isEmpty(question)) {
            or.andQuestionLike("%" + question + "%");
        }

        //过滤逻辑删除
        or.andLogicalDeleted(true);

        //排序
        marketIssueExample.orderBy(sort + " " + order);

        //分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        //调用select方法
        List<MarketIssue> marketIssues = issueMapper.selectByExample(marketIssueExample);
        session.commit();
        session.close();

        //返回数据
        return marketIssues;
    }

    //create方法
    @Override
    public MarketIssue create(String question, String answer) {
        //获得session
        SqlSession session = MybatisUtil.getSession();
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);
        MarketIssue marketIssue = new MarketIssue();

        //存入信息
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        marketIssue.setAddTime(LocalDateTime.now());
        marketIssue.setUpdateTime(LocalDateTime.now());
        marketIssue.setDeleted(false);

        //插入数据
        issueMapper.insert(marketIssue);
        session.commit();
        session.close();

        //返回数据
        return marketIssue;
    }

    //update方法
    @Override
    public MarketIssue update(String question, String answer, String id, String addTime, String deleted) {
        //获得session
        SqlSession session = MybatisUtil.getSession();
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);
        MarketIssue marketIssue = new MarketIssue();

        //存入信息
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(addTime, formatter);
        marketIssue.setAddTime(dateTime);
        marketIssue.setUpdateTime(LocalDateTime.now());
        marketIssue.setId(Integer.valueOf(id));
        marketIssue.setDeleted(false);

        //更新信息
        issueMapper.updateByPrimaryKeySelective(marketIssue);
        session.commit();
        session.close();

        //返回数据
        return marketIssue;

    }

    //delete方法
    @Override
    public void delete(String id) {
        //获得session
        SqlSession session = MybatisUtil.getSession();
        MarketIssueMapper issueMapper = session.getMapper(MarketIssueMapper.class);
        //添加逻辑删除数据
        issueMapper.logicalDeleteByPrimaryKey(Integer.valueOf(id));
        session.commit();
        session.close();
    }
}
